package com.yesep.learn.netty.udp.client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;

public class UDPSimpleClient {

    public static void send() throws IOException {
        String str = "QOTM?";
        byte[] data = str.getBytes("UTF-8");
        DatagramSocket client = new DatagramSocket();
        client.setSoTimeout(3000);//超时时间
        InetAddress address = InetAddress.getByName("127.0.0.1");

        System.out.println("send:" + str);
        DatagramPacket sendPacket = new DatagramPacket(data, data.length, address, 8050);
        client.send(sendPacket);//发送

        byte[] buffer = new byte[1024];
        DatagramPacket recvPacket = new DatagramPacket(buffer, buffer.length);
        client.receive(recvPacket);//接收
        int len = recvPacket.getLength();
        byte[] recvBuf = recvPacket.getData();
        System.out.println("receive:" + new String(recvBuf, "UTF-8"));
    }

    public static void main(String[] args) throws IOException {
        send();
    }
}
